package com.tandn;

import java.util.Vector;

import com.tandn.activity.show.ShowListAcitvity;
import com.tandn.activity.show.ShowMapActivity;
import com.tandn.models.ATM;
import com.tandn.models.Bank;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ATMLocatorActivity extends Activity
{
	private EditText _bankEditText = null;
	private EditText _cityEditText = null;
	private EditText _districtEditText = null;

	private TextView _bankResultTextView = null;
	private TextView _cityResultTextView = null;
	private TextView _districtResultTextView = null;

	private Button _showResultsButton = null;
	private Button _updateDataButton = null;
	private Button _searchInMapButton = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_search);

		_bankEditText = (EditText) findViewById(R.id.editTextBank);
		_cityEditText = (EditText) findViewById(R.id.editTextCity);
		_districtEditText = (EditText) findViewById(R.id.editTextDistrict);

		_bankResultTextView = (TextView) findViewById(R.id.textViewResultBank);
		_cityResultTextView = (TextView) findViewById(R.id.textViewResultCity);
		_districtResultTextView = (TextView) findViewById(R.id.textViewResultDistrict);

		_showResultsButton = (Button) findViewById(R.id.buttonShowResults);
		_updateDataButton = (Button) findViewById(R.id.buttonUpdateData);
		_searchInMapButton = (Button) findViewById(R.id.buttonSearchInMap);

		/* If there are not any ATM */
		if (AppMain._numberSearchATMWithBank == 0)
		{
			_cityEditText.setEnabled(false);
			_districtEditText.setEnabled(false);
			_showResultsButton.setEnabled(false);
			_searchInMapButton.setEnabled(false);
		}

		_bankEditText.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count)
			{
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after)
			{
			}

			public void afterTextChanged(Editable s)
			{
				AppMain._numberSearchATMWithBank = searchATMWithBank(s.toString());
				_bankResultTextView.setText(AppMain._numberSearchATMWithBank
						+ " ATM");
				if (AppMain._numberSearchATMWithBank > 0)
				{
					_cityEditText.setEnabled(true);
					_searchInMapButton.setEnabled(true);
				}
				else
				{
					_cityEditText.setEnabled(false);
				}
			}
		});

		_cityEditText.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count)
			{
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after)
			{
			}

			public void afterTextChanged(Editable s)
			{
				AppMain._numberSearchATMWithCity = searchATMWithCity(s.toString());
				_cityResultTextView.setText(AppMain._numberSearchATMWithCity
						+ " ATM");
				if (AppMain._numberSearchATMWithCity > 0)
				{
					_showResultsButton.setEnabled(true);
					_districtEditText.setEnabled(true);
				}
				else
				{
					_showResultsButton.setEnabled(false);
					_districtEditText.setEnabled(false);
				}
			}
		});

		_districtEditText.addTextChangedListener(new TextWatcher() {

			public void onTextChanged(CharSequence s, int start, int before,
					int count)
			{
			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after)
			{
			}

			public void afterTextChanged(Editable s)
			{
				AppMain._numberSearchATMWithDistrict = searchATMWithDistrict(s.toString());
				_districtResultTextView.setText(AppMain._numberSearchATMWithDistrict
						+ " ATM");
			}
		});

		_showResultsButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v)
			{
				if (AppMain.isNetworkAvailable(ATMLocatorActivity.this))
				{
					Log.d("Debug", "111111111111 yes");
					AppMain.showMessageBox(ATMLocatorActivity.this, getString(R.string.textNetworkAvailable));
					Intent intent = new Intent(ATMLocatorActivity.this,
							ShowMapActivity.class);
					startActivity(intent);
				}
				else
				{
					Log.d("Debug", "111111111111 NO");
					AppMain.showMessageBox(ATMLocatorActivity.this, getString(R.string.textNetworkNotAvailable));

					/* Start activity */
					Intent intent = new Intent(ATMLocatorActivity.this,
							ShowListAcitvity.class);
					startActivity(intent);
				}
			}
		});

		_searchInMapButton.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0)
			{
				Intent intent = new Intent(ATMLocatorActivity.this,
						ATMLocatorInMapAcivity.class);
				startActivity(intent);
			}
		});

		_updateDataButton.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0)
			{
			}
		});

		/* If data can not be loaded */
		if (AppMain._XmlParserHandler.getBanksList() == null)
		{
			_bankEditText.setEnabled(false);
			new Thread(new Runnable() {

				public void run()
				{
					while (true)
					{
						Log.d("Debug", "read data ....");
						try
						{
							AppMain.readXMLData(getAssets().open("data.xml"));
						} catch (Exception e)
						{
							e.printStackTrace();
							continue;
						}
						if (AppMain._XmlParserHandler.getBanksList() != null)
						{
							_bankEditText.setEnabled(true);
							break;
						}
						else
						{
							AppMain.showMessageBox(ATMLocatorActivity.this, getString(R.string.textLoadingData));
						}
					}
				}
			}).start();
		}
	}

	public void loadData()
	{
		try
		{
			AppMain.readXMLData(getAssets().open("data.xml"));
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private int searchATMWithBank(String bankSearchWord)
	{
		if (AppMain._XmlParserHandler.getBanksList() == null)
			return 0;
		for (Bank bank : AppMain._XmlParserHandler.getBanksList())
		{
			if (bank.getName().equalsIgnoreCase(bankSearchWord)
					|| bank.getShortcut().equalsIgnoreCase(bankSearchWord))
			{
				AppMain._listATMSearchWithBank = bank.getATMsList();
				AppMain._bankName = bank.getName();
				return AppMain._listATMSearchWithBank.size();
			}
		}
		return 0;
	}

	private int searchATMWithCity(String citySearchWord)
	{
		if (AppMain._XmlParserHandler.getBanksList() == null)
			return 0;
		AppMain._listATMSearchWithCity = new Vector<ATM>();
		for (ATM atm : AppMain._listATMSearchWithBank)
		{
			if (atm.getCity().equalsIgnoreCase(citySearchWord))
			{
				AppMain._listATMSearchWithCity.add(atm);
			}
		}
		if (AppMain._listATMSearchWithCity.size() == 0)
		{
			AppMain._listATMSearchWithCity = null;
			return 0;
		}
		return AppMain._listATMSearchWithCity.size();
	}

	private int searchATMWithDistrict(String districtSearchWord)
	{
		if (AppMain._XmlParserHandler.getBanksList() == null)
			return 0;
		AppMain._listATMSearchWithDistrict = new Vector<ATM>();
		for (ATM atm : AppMain._listATMSearchWithCity)
		{
			if (atm.getDistrict().equalsIgnoreCase(districtSearchWord))
			{
				AppMain._listATMSearchWithDistrict.add(atm);
			}
		}
		if (AppMain._listATMSearchWithDistrict.size() == 0)
		{
			AppMain._listATMSearchWithDistrict = null;
			return 0;
		}
		return AppMain._listATMSearchWithDistrict.size();
	}

	// @Override
	// public boolean onKeyDown(int keyCode, KeyEvent event)
	// {
	// Intent intent = new Intent(this, ShowList.class);
	// startActivity(intent);
	// return super.onKeyDown(keyCode, event);
	// }

}
